package com.lxkpt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxkpt.bean.Business;
import com.lxkpt.bean.User;
import com.lxkpt.service.BusinessService;
import com.lxkpt.service.UserService;
import com.lxkpt.vo.BusinessVo;
import com.lxkpt.voutil.BusinessVoUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家管理
 */
@Controller
@RequestMapping("/businessManage")
public class BusinessManageController {

    Map<Object, Object> map = new HashMap<>();

    @Resource
    private BusinessService businessService;

    @Resource
    private UserService userService;


    @RequestMapping(value = "/addBusiness", method = RequestMethod.GET)
    @RequiresRoles("admin")
    public String addBusiness() {
        return "business-add";
    }


    /**
     * 新增商家
     *
     * @param business
     * @return
     */
    @RequestMapping(value = "/addBusiness", method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public Object addBusiness(Business business) {
        int result = businessService.insertSelective(business);
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 搜索商家
     *
     * @param pn
     * @param type
     * @param content
     * @param model
     * @return
     */
    @RequestMapping("/searchBusiness")
    @RequiresRoles("admin")
    public String searchUser(@RequestParam(value = "pn", defaultValue = "1") String pn,
                             @RequestParam(value = "type") String type,
                             @RequestParam(value = "content") String content,
                             Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        Business business = new Business();
        List<Business> businessList = null;
        PageHelper.startPage(pageNo, 10);
        switch (type) {
            case "bnName":
                business.setBnName(content);
                businessList = businessService.searchBusiness(business);
                break;
            case "bnPhone":
                business.setBnPhone(content);
                businessList = businessService.searchBusiness(business);
                break;
            case "bnLegalPerson":
                business.setBnLegalPerson(content);
                businessList = businessService.searchBusiness(business);
                break;
            case "bnType":
                business.setBnType(content);
                businessList = businessService.searchBusiness(business);
                break;
            case "bnCode":
                business.setBnCode(content);
                businessList = businessService.searchBusiness(business);
                break;
            default:
                break;
        }
        PageInfo pageInfo = new PageInfo(businessList);
        List<BusinessVo> businessVo = BusinessVoUtil.getBusinessVo(businessList, userService);
        pageInfo.setList(businessVo);
        model.addAttribute("pageInfo", pageInfo);
        return "business-search";
    }

    /**
     * 更新商家
     *
     * @param business
     * @return
     */
    @RequestMapping("/updateBusiness")
    @RequiresRoles("admin")
    @ResponseBody
    public Object updateBusiness(Business business) {
        int result = businessService.updateByPrimaryKeySelective(business);
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 删除商家
     *
     * @param bnId
     * @return
     */
    @RequestMapping("/deleteBusiness")
    @RequiresRoles("admin")
    @ResponseBody
    public Object deleteBusiness(@RequestParam(value = "bnId") int bnId) {
        int result = businessService.deleteByPrimaryKey(bnId);
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 编辑商家
     *
     * @param bnId
     * @return
     */
    @RequestMapping("/getBusiness")
    @RequiresRoles("admin")
    public String getBusiness(Model model, @RequestParam(value = "bnId") int bnId) {
        Business business = businessService.selectByPrimaryKey(bnId);
        if (business != null) {
            BusinessVo businessVo = new BusinessVo();
            User user = new User();
            user.setUserId(business.getBnAuthorId());
            User u = userService.selectOneUser(user);
            businessVo.setBnAuthorName(u.getUserName());
            businessVo.setBusiness(business);
            model.addAttribute("businessVo", businessVo);
        }
        return "business-update";
    }

    /**
     * 商家列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/businessList")
    @RequiresRoles("admin")
    public String businessList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        List<Business> businessList = businessService.selectBusiness(null);
        PageInfo pageInfo = new PageInfo(businessList);
        List<BusinessVo> businessVo = BusinessVoUtil.getBusinessVo(businessList, userService);
        pageInfo.setList(businessVo);
        model.addAttribute("pageInfo", pageInfo);
        return "business-manage";
    }
}
